<?php

namespace App\Console\Commands;

use App\Library\Logic\ShopProductLogic;
use Illuminate\Console\Command;

class SyncProductCreatedTimeCommand extends Command
{
    /**
     * 同步发布时间和创建时间
     *
     * @var string
     */
    protected $signature = 'command:createdTime';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $product = ShopProductLogic::query()->whereNull('publish_at')->get();
        foreach ($product as $key => $value) {
            if ($value->product_online_time > 0) {
                $date = now()->timestamp($value->product_online_time)->startOfDay()->format('Y-m-d');
                ShopProductLogic::query()->where('product_id', $value->product_id)->update([
                    'publish_at' => now()->parse($date)->subDays(15),
                    'product_online_time' => strtotime(now()->parse(date('Y-m-d H:i:s', $value->product_online_time))->subDays(15))
                ]);
            }
        }

        $productNoCreatedTime = ShopProductLogic::query()->whereNull('created_time')->get();
        foreach ($productNoCreatedTime as $item) {
            ShopProductLogic::query()->where('product_id', $item->product_id)->update(['created_time' => date('Y-m-d', strtotime($item->add_at))]);
        }
    }
}
